<!DOCTYPE html>
<html>
    <head>
        <script src="/w3c/resources/testharness.js"></script>
        <script src="/w3c/resources/testharnessreport.js"></script>
        <script src="mediasource-util.js"></script>

        <link rel='stylesheet' href='/w3c/resources/testharness.css'>
    </head>
    <body>
        <div id="log"></div>
        <script>

          // NOTE: These tests should not be upstreamed with WPT.
          // The following tests cover Chrome-specific QuotaExceededError behavior. The spec allows
          // UAs flexibility for when to throw this exception.

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              test.expectEvent(sourceBuffer, "updateend");
              sourceBuffer.appendBuffer(mediaData);
              test.waitForExpectedEvents(function()
              {
                  assert_throws_dom("QuotaExceededError",
                      function() { mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE); },
                      "addSourceBuffer must throw an exception if the MediaSource has already received init segments for all sourcebuffers added at the time");
                  test.done();
              });
          }, "Test addSourceBuffer() throws QuotaExceededError after MediaSource has completed init.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              test.expectEvent(sourceBuffer, "updateend");
              // Append incomplete init segment to the first SourceBuffer.
              sourceBuffer.appendBuffer(initSegment.slice(0, initSegment.length - 1));
              test.waitForExpectedEvents(function()
              {
                  // We have not added a full init segment for the first SourceBuffer yet, so MediaSource init is not yet
                  // finished and we should be able to add/remove one more SourceBuffer.
                  var sourceBuffer2 = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
                  mediaSource.removeSourceBuffer(sourceBuffer2);

                  // Now append the rest of the init segment, this should complete the initialization of the MediaSource.
                  test.expectEvent(sourceBuffer, "updateend");
                  test.expectEvent(mediaElement, "loadedmetadata", "mediaElement metadata.");
                  sourceBuffer.appendBuffer(initSegment.slice(initSegment.length - 1));
              });

              test.waitForExpectedEvents(function()
              {
                  // MediaSource is fully initialized now, so adding new SourceBuffers is no longer possible.
                  assert_equals(mediaElement.readyState, HTMLMediaElement.HAVE_METADATA);
                  assert_throws_dom("QuotaExceededError",
                      function() { mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE); },
                      "addSourceBuffer must throw an exception if the media element has already reached HAVE_METADATA");
                  test.done();
              });
          }, "Test addSourceBuffer() succeeds while incomplete init segment is appended, fails after MediaSource init completed.");
        </script>
    </body>
</html>
